package com.opensource.svgaplayer.bitmap;

import com.opensource.svgaplayer.utils.log.LogUtil;
import ohos.media.image.ImageSource;
import ohos.media.image.common.Size;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-28
 */
public class BitmapSampleSizeCalculator {
    private static BitmapSampleSizeCalculator INSTANCE;

    public static BitmapSampleSizeCalculator getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new BitmapSampleSizeCalculator();
        }
        return INSTANCE;
    }

    /**
     * calculate
     *
     * @param realSize realSize
     * @param options  realSize
     * @return int
     */
    public int calculate(Size realSize, ImageSource.DecodingOptions options) {
        int width = realSize.width;
        int height = realSize.height;
        int reqHeight = options.desiredSize.height;
        int reqWidth = options.desiredSize.width;
        int inSampleSize = 1;
        if (reqHeight > 0 && reqWidth > 0) {
            if (height > reqHeight || width > reqWidth) {
                int halfHeight = height / 2;

                for (int halfWidth = width / 2; halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth; inSampleSize *= 2) {
                }
            }
            LogUtil.loge("calculate-----------" + width + "/" + height + "/" + reqWidth + "/" + reqWidth + "/" + inSampleSize);
            return inSampleSize;
        } else {
            return inSampleSize;
        }
    }

    private BitmapSampleSizeCalculator() {
    }
}
